package com.kelvin.backend.common.exception;

import com.kelvin.backend.common.web.response.BaseCode;
import com.kelvin.backend.common.web.response.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public BaseResponse defaultExceptionHandler(Exception e) {
        log.error("[defaultExceptionHandler] 系统异常 -> {}", e.getMessage(), e);
        return BaseResponse.response(BaseCode.ERROR);
    }

    @ExceptionHandler(BaseException.class)
    public BaseResponse baseExceptionHandler(BaseException e) {
        log.info("[baseExceptionHandler] 系统异常 -> {}", e.getMessage(), e);
        return BaseResponse.response(e.code, e.msg);
    }
}